// logs.ts
// const util = require('../../utils/util.js')
import { formatTime } from '../../utils/util'
// const app = getApp()
var bgm = wx.getBackgroundAudioManager()

Page({
  data: {
    logs: [],
    active: 1,
    music: {},
    isPlay: false,
    currentTab: "热歌榜"
  },
  onLoad() {
    this.setData({
      logs: (wx.getStorageSync('logs') || []).map((log: string) => {
        return formatTime(new Date(log))
        // return {
        //   date: util.formatTime(new Date(log)),
        //   timeStamp: log
        // }
      })
    })
    this.getList("热歌榜")
  },
  onChange(event: { detail: { name: any; }; }) {
    this.getList(event.detail.name || this.data.currentTab)
    // app.sesrchDataCallback = (res: any) => {
    // console.log(res)
    // }
    event.detail.name && this.setData({
      currentTab: event.detail.name
    })
    event.detail.name && wx.showToast({
      title: `切换到 ${event.detail.name}`,
      icon: 'none',
    });
    // await this.playMusic()
    // var bgm = wx.getBackgroundAudioManager()
    // var music = this.data.music as any
    // bgm.title = music.name || '告白';
    // bgm.epname = music.name || '告白';
    // bgm.singer = music.artistsname || '沈以诚';
    // bgm.coverImgUrl = music.picurl || 'http://p3.music.126.net/4xF8d6QNMy-TKYMy2AUwCQ==/109951163803974764.jpg';
    // // 设置了 src 之后会自动播放
    // bgm.src = music.url || 'http://music.163.com/song/media/outer/url?id=1336856449';
    // bgm.play();
  },
  async next(event: { detail: { name: any } }) {
    this.onChange(event)
    await this.playMusic()
  },
  getList: function (name: string) {
    bgm.pause();
    // 利用小程序内置的发送请求的方法
    wx.request({
      url: `https://api.uomg.com/api/rand.music?sort=${name}&format=json`, //接口地址
      success: (res) => {
        // console.log(res)
        let resData = res.data as any
        if (res.errMsg = "request:ok") {
          // console.log(resData.data)
          var music = resData.data
          this.setData({
            music,
            isPlay: true
          });
          bgm.title = music.name;
          bgm.epname = music.name;
          bgm.singer = music.artistsname;
          bgm.coverImgUrl = music.picurl;
          // 设置了 src 之后会自动播放
          bgm.src = music.url
          console.log(this.data.isPlay)
          this.data.isPlay ? bgm.play() : bgm.pause();
          // return resData.data
        }
        // if (app.searchDataCallback) {
        //   app.searchDataCallback(resData)
        // }
      } // 请求成功的回调函数
    })
    // return null
  },

  playMusic(_ev?: any) {
    var isPlay = this.data.isPlay;
    var music = this.data.music as any;
    console.log("playMusic", music)
    if (isPlay) {
      bgm.pause();
      // wx.pauseBackgroundAudio();
      this.setData({
        isPlay: false
      })
    } else {
      bgm.title = music.name || '告白';
      bgm.epname = music.name || '告白';
      bgm.singer = music.artistsname || '沈以诚';
      bgm.coverImgUrl = music.picurl || 'http://p3.music.126.net/4xF8d6QNMy-TKYMy2AUwCQ==/109951163803974764.jpg';
      // 设置了 src 之后会自动播放
      bgm.src = music.url || 'http://music.163.com/song/media/outer/url?id=1336856449';
      bgm.play();
      // wx.playBackgroundAudio({
      //   dataUrl: 'http://music.163.com/song/media/outer/url?id=1336856449',
      //   title: '告白',
      //   coverImgUrl: 'http://p3.music.126.net/4xF8d6QNMy-TKYMy2AUwCQ==/109951163803974764.jpg'
      // })
      this.setData({
        isPlay: true
      })
    }
  }
})
